<?php

namespace Yolo\Errors;

class ExceptionRegistry
{
    private static ?ExceptionRegistry $self = null;

    private array $moduleTypes = [];

    /**
     * @var ResponseError[]
     */
    private array $exceptions = [];

    private function __construct() {}

    public static function inst(): ExceptionRegistry
    {
        if (is_null(self::$self)) {
            self::$self = new self();
        }

        return self::$self;
    }

    public function createExceptionRegistry(string $module, array $types): void
    {
        $this->moduleTypes = array_merge($this->moduleTypes, array_map(function ($type) use ($module) {

            $type['module'] = $module;
            return $type;
        }, $types));
    }

    public function getModuleTypes(): array
    {
        return $this->moduleTypes;
    }

    public function register(ResponseError $exception): void
    {
        $this->exceptions[$exception->getSymbol()] = $exception;
    }

    /**
     * @param string $symbol
     * @return ResponseError|null
     */
    public function get(string $symbol): ?ResponseError
    {
        return $this->exceptions[$symbol] ?? null;
    }

    public static function initSystemErrors(): void
    {
        require_once __DIR__ . '/errors.php';
    }
}
